package Fragments;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.coolweather.lechat.R;
import com.tencent.imsdk.conversation.Conversation;
import com.tencent.imsdk.v2.V2TIMConversation;
import com.tencent.imsdk.v2.V2TIMConversationListener;
import com.tencent.imsdk.v2.V2TIMConversationResult;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMValueCallback;

import java.util.ArrayList;
import java.util.List;

import activity.SendNews;

public class FragmentChat extends Fragment {
    private static final String TAG = "FragmentChat";
    private View view;
    private RecyclerView recyclerView;
    private ChatAdapter chatAdapter;
    private static List<V2TIMConversation> list=new ArrayList<>();

    static class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
        private Context context;

        public ChatAdapter(Context context) {
            this.context = context;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_item,parent,false);
            return new ChatViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            V2TIMConversation v2TIMConversation =list.get(position);
            FragmentChat.ChatAdapter.ChatViewHolder chatViewHolder =(ChatViewHolder)holder;
            chatViewHolder.item_name.setText(v2TIMConversation.getUserID());
            chatViewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, SendNews.class);
                    intent.putExtra("userId",v2TIMConversation.getUserID());
                    context.startActivity(intent);
                }
            });

        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        static class ChatViewHolder extends RecyclerView.ViewHolder{

            private ImageView item_img_tou;
            private TextView item_name;
            private View mView;

            public ChatViewHolder(@NonNull View view) {
                super(view);
                item_img_tou =view.findViewById(R.id.item_img_tou);
                item_name = view.findViewById(R.id.item_name);
                mView = view;
            }
        }
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_chat,container,false);
        chatAdapter = new ChatAdapter(getActivity());
        initview();
        return view;
    }

    private void initview() {
        recyclerView = view.findViewById(R.id.recyclerView);
        Log.e(TAG, "initView:"+recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(chatAdapter);
        V2TIMManager.getConversationManager().setConversationListener(new V2TIMConversationListener() {
            @Override
            public void onSyncServerStart() {
                super.onSyncServerStart();
            }

            @Override
            public void onSyncServerFinish() {
                super.onSyncServerFinish();
            }

            @Override
            public void onSyncServerFailed() {
                super.onSyncServerFailed();
            }

            @Override
            public void onNewConversation(List<V2TIMConversation> conversationList) {
                super.onNewConversation(conversationList);
            }

            @Override
            public void onConversationChanged(List<V2TIMConversation> conversationList) {
                super.onConversationChanged(conversationList);
            }
        });
        V2TIMManager.getConversationManager().getConversationList(0, 100, new V2TIMValueCallback<V2TIMConversationResult>() {
            @Override
            public void onError(int i, String s) {

            }

            @Override
            public void onSuccess(V2TIMConversationResult v2TIMConversationResult) {
                Log.e(TAG, "onSuccess:"+v2TIMConversationResult.getConversationList());
                list = v2TIMConversationResult.getConversationList();
                chatAdapter.notifyDataSetChanged();
            }
        });

    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
}
